package com.example.shopping_web_back.service;

import com.example.shopping_web_back.mapper.BrowsingHistoryMapper;
import com.example.shopping_web_back.pojo.BrowsingHistory;
import com.example.shopping_web_back.pojo.PageBean;
import com.example.shopping_web_back.service.impl.BrowsingHistoryServiceImpl;
import com.example.shopping_web_back.utils.ThreadLocalUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class BrowsingHistoryService implements BrowsingHistoryServiceImpl {

    @Autowired
    private BrowsingHistoryMapper browsingHistoryMapper;

    @Override
    public PageBean<BrowsingHistory> getBrowsingHistory(Integer pageNum, Integer pageSize) {
        //1.创建PageBean
        PageBean<BrowsingHistory> pbBW = new PageBean<>();
        Map<String, Object> map = ThreadLocalUtil.get();
        String businessName = (String) map.get("Business_name");

        //2.开启分页查询
        PageHelper.startPage(pageNum, pageSize);

        //3.调用mapper
        List<BrowsingHistory> browsingHistoryList =browsingHistoryMapper.list(businessName);
        PageInfo<BrowsingHistory> browsingHistory = new PageInfo<BrowsingHistory>(browsingHistoryList);
        pbBW.setTotal(browsingHistory.getTotal());
        pbBW.setItems(browsingHistory.getList());
        return pbBW;
    }

    @Override
    public void addBrowsingHistory(BrowsingHistory browsingHistory) {
        browsingHistoryMapper.addBrowsingHistory(browsingHistory);
    }

    @Override
    public void deleteBrowsingHistory(BrowsingHistory browsingHistory) {
        browsingHistoryMapper.deleteBrowsingHistory(browsingHistory);
    }
}
